//
//  DMLifecycleTypes.m
//  DualMediationSDK
//
//  生命周期管理相关类型定义实现
//

#import "DMLifecycleTypes.h"

#pragma mark - 常量定义

// 通知 & UserInfo Key
NSString * const DMLifecycleManagerDidChangeStateNotification = @"DMLifecycleManagerDidChangeStateNotification";
NSString * const DMLifecycleManagerDidReceiveEventNotification = @"DMLifecycleManagerDidReceiveEventNotification";
NSString * const DMLifecycleManagerStateKey = @"DMLifecycleManagerStateKey";
NSString * const DMLifecycleManagerEventKey = @"DMLifecycleManagerEventKey";
NSString * const DMLifecycleManagerUserInfoKey = @"DMLifecycleManagerUserInfoKey";

// 代理优先级
const NSInteger DMLifecycleDelegatePriorityHigh = 1000;
const NSInteger DMLifecycleDelegatePriorityNormal = 500;
const NSInteger DMLifecycleDelegatePriorityLow = 0;

// 错误域
NSString * const DMLifecycleErrorDomain = @"DMLifecycleErrorDomain";

#pragma mark - 工具函数实现

NSString *DMLifecycleStateDescription(DMLifecycleState state) {
    switch (state) {
        case DMLifecycleStateUnknown:
            return @"Unknown";
        case DMLifecycleStateLaunching:
            return @"Launching";
        case DMLifecycleStateActive:
            return @"Active";
        case DMLifecycleStateInactive:
            return @"Inactive";
        case DMLifecycleStateForeground:
            return @"Foreground";
        case DMLifecycleStateBackground:
            return @"Background";
        case DMLifecycleStateSuspended:
            return @"Suspended";
        case DMLifecycleStateTerminating:
            return @"Terminating";
        case DMLifecycleStateTerminated:
            return @"Terminated";
        default:
            return [NSString stringWithFormat:@"Unknown(%ld)", (long)state];
    }
}

NSString *DMLifecycleEventDescription(DMLifecycleEvent event) {
    switch (event) {
        case DMLifecycleEventUnknown:
            return @"Unknown";
        case DMLifecycleEventDidFinishLaunching:
            return @"DidFinishLaunching";
        case DMLifecycleEventDidBecomeActive:
            return @"DidBecomeActive";
        case DMLifecycleEventWillResignActive:
            return @"WillResignActive";
        case DMLifecycleEventDidEnterBackground:
            return @"DidEnterBackground";
        case DMLifecycleEventWillEnterForeground:
            return @"WillEnterForeground";
        case DMLifecycleEventWillTerminate:
            return @"WillTerminate";
        case DMLifecycleEventDidReceiveMemoryWarning:
            return @"DidReceiveMemoryWarning";
        case DMLifecycleEventDidReceiveRemoteNotification:
            return @"DidReceiveRemoteNotification";
        case DMLifecycleEventDidReceiveLocalNotification:
            return @"DidReceiveLocalNotification";
        case DMLifecycleEventOpenURL:
            return @"OpenURL";
        case DMLifecycleEventContinueUserActivity:
            return @"ContinueUserActivity";
        default:
            return [NSString stringWithFormat:@"Unknown(%ld)", (long)event];
    }
}

BOOL DMLifecycleIsValidStateTransition(DMLifecycleState fromState, DMLifecycleState toState) {
    // 相同状态转换总是有效的
    if (fromState == toState) {
        return YES;
    }
    
    // 从Unknown状态可以转换到任何状态
    if (fromState == DMLifecycleStateUnknown) {
        return YES;
    }
    
    // 定义有效的状态转换规则
    switch (fromState) {
        case DMLifecycleStateLaunching:
            // 应用启动时可以转换到Inactive、Active或Foreground
            return (toState == DMLifecycleStateInactive || 
                    toState == DMLifecycleStateActive ||
                    toState == DMLifecycleStateForeground);
            
        case DMLifecycleStateInactive:
            // 从Inactive可以转换到Active、Foreground、Background或Terminating
            return (toState == DMLifecycleStateActive || 
                    toState == DMLifecycleStateForeground ||
                    toState == DMLifecycleStateBackground ||
                    toState == DMLifecycleStateTerminating);
            
        case DMLifecycleStateActive:
            // 从Active可以转换到Inactive、Foreground、Background或Terminating
            return (toState == DMLifecycleStateInactive || 
                    toState == DMLifecycleStateForeground ||
                    toState == DMLifecycleStateBackground ||
                    toState == DMLifecycleStateTerminating);
            
        case DMLifecycleStateForeground:
            // 从Foreground可以转换到Active、Inactive、Background或Terminating
            return (toState == DMLifecycleStateActive ||
                    toState == DMLifecycleStateInactive ||
                    toState == DMLifecycleStateBackground ||
                    toState == DMLifecycleStateTerminating);
            
        case DMLifecycleStateBackground:
            // 从Background可以转换到Inactive、Foreground、Suspended或Terminating
            return (toState == DMLifecycleStateInactive || 
                    toState == DMLifecycleStateForeground ||
                    toState == DMLifecycleStateSuspended ||
                    toState == DMLifecycleStateTerminating);
            
        case DMLifecycleStateSuspended:
            // 从Suspended可以转换到Background、Foreground或Terminated
            return (toState == DMLifecycleStateBackground ||
                    toState == DMLifecycleStateForeground ||
                    toState == DMLifecycleStateTerminated);
            
        case DMLifecycleStateTerminating:
            // 从Terminating可以转换到Terminated
            return (toState == DMLifecycleStateTerminated);
            
        case DMLifecycleStateTerminated:
            // 从Terminated状态不能转换到其他状态
            return NO;
            
        default:
            // 未知状态，允许转换
            return YES;
    }
}